import json
from threading import Timer
import datetime



'''
    log_format  main '$remote_addr - $remote_user [$time_local] "$request" '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                        '"$request_time" $upstream_response_time';
'''



point = 0
config = json.loads(open(r'./checkFile.json', encoding='utf-8').read())
file = config["file"]
log = config["logPath"]


def doMain(inc):
    print("检索更新的请求日志中······")
    global point
    f = open(file, encoding='utf-8')
    f.seek(point)
    lines = f.readlines()
    point = f.tell()
    file2 = open(log + getNow() + '.log', "a")
    if len(lines) > 0:
        file2.write("接受了请求："+str(len(lines))+"次\n")
    else:
        file2.write("这一秒没有接收请求\n")
    file2.close()
    t = Timer(inc, doMain, (inc,))
    t.start()


def getNow():
    return str(datetime.datetime.now().strftime('%Y-%m-%d %H_%M'))


def init():
    global point
    f = open(file, encoding='utf-8')
    f.seek(point)
    f.readlines()
    point = f.tell()


init()
doMain(1)
